package mspbots.qbd.desktop.core.profitandloss;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.log4j.Log4j2;
import lombok.val;
import mspbots.qbd.common.RabbitKeys;
import mspbots.qbd.desktop.core.BaseGenericService;
import mspbots.qbd.desktop.utils.XmlResponseUtils;
import org.springframework.stereotype.Service;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import reactor.core.publisher.Flux;

@Log4j2
@Service
public class InvoiceService extends BaseGenericService {

    public void sendInvoice(NodeList nodeList, Long tenantId) {

        ArrayNode jsonNodes = XmlResponseUtils.parseXmlArrNodes(nodeList,tenantId);
        log.info("Invoice:jsonNodes:{}",jsonNodes.size());

        Flux.fromIterable(jsonNodes)
                .subscribe(objectNode -> this.messagingTemplate.convertAndSend(RabbitKeys.BASE_EXCHANGE_NAME,
                                            RabbitKeys.INVOICE_SYNC_QUEUE_KEY, objectNode));

    }
}
